package com.fanbai.score.common.Utils.pop;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.net.Uri;
import android.provider.MediaStore;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;


import com.fanbai.score.R;
import com.fanbai.score.common.Utils.CommonUtil;
import com.fanbai.score.common.Utils.OtherUtils;
import com.fanbai.score.common.Utils.log.YD_Log;

import java.io.File;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Locale;

public class PopPhotoWin implements OnClickListener {

	private static final int PHOTO_REQUEST_TAKEPHOTO = 1;// 拍照
	private static final int PHOTO_REQUEST_GALLERY = 2;// 从相册中选择

	private File photoDir = new File(OtherUtils.photoDir);
	private String photoPath;
	private Activity ctx;
	private View view;
	private PopupWindowUtils popWin;
	private TextView tvNew, tvPhoto, tvCancle;

	/**
	 * 构造
	 * 
	 * @param ctx
	 */
	public PopPhotoWin(Activity ctx) {
		this.ctx = ctx;
		initView();
		if (popWin == null)
			popWin = new PopupWindowUtils(ctx).setContentView(view);
	}

	/**
	 * 显示
	 * 
	 * @param parent
	 */
	public void show(View parent) {
		popWin.show(parent, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
	}

	/**
	 * 结束后，获取照片文件(请求码1，2)
	 */
	public void getResult(int requestCode, Intent data, final PopCallBack<String> callBack) {
		if (callBack == null)// 非空判断
			return;
		switch (requestCode) {
		case PHOTO_REQUEST_TAKEPHOTO:// 当选择拍照时调用
			callBack.onResult(photoPath);
			break;
		case PHOTO_REQUEST_GALLERY:// 当选择从本地获取图片时
			try {
				photoPath = CommonUtil.getPathFromUri(ctx, data.getData()); // 获得图片的uri
				callBack.onResult(photoPath);
			} catch (Exception e) {
				e.printStackTrace();
			}
			break;
		}
	}

	private void initView() {
		view = View.inflate(ctx, R.layout.pop_menu, null);
		tvNew = (TextView) view.findViewById(R.id.tv_new);
		tvNew.setText("拍照");
		tvPhoto = (TextView) view.findViewById(R.id.tv_photo);
		tvPhoto.setText("从相册选择");
		tvCancle = (TextView) view.findViewById(R.id.tv_cancel);
		tvNew.setOnClickListener(this);
		tvPhoto.setOnClickListener(this);
		tvCancle.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		if (v == tvNew) {
			if (!photoDir.isDirectory()) {
				photoDir.mkdirs();
			}
			photoPath = OtherUtils.photoDir + "/" + getPhotoFileName();
			Intent cameraintent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
			// 指定调用相机拍照后照片的储存路径
			cameraintent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(photoPath)));
			ctx.startActivityForResult(cameraintent, PHOTO_REQUEST_TAKEPHOTO);
		} else if (v == tvPhoto) {
			Intent getAlbum = new Intent(Intent.ACTION_GET_CONTENT);
			getAlbum.setType("image/*");
			ctx.startActivityForResult(getAlbum, PHOTO_REQUEST_GALLERY);
		} else if (v == tvCancle) {

		}
		popWin.dismiss();
	}

	/**
	 * 为了适配三星手机,需要进行如下操作：
	 * 清单文件Activity添加android:configChanges="keyboardHidden|orientation|screenSize";
	 * 重写onConfigurationChanged并调用此方法
	 * 
	 * @param newConfig
	 */
	public void onConfigurationChanged(Configuration newConfig) {
		YD_Log.i("UserInfoActivity", "onConfigurationChanged");
		if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
			YD_Log.i("UserInfoActivity", "横屏");
			Configuration o = newConfig;
			o.orientation = Configuration.ORIENTATION_PORTRAIT;
			newConfig.setTo(o);
		} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
			YD_Log.i("UserInfoActivity", "竖屏");
		}
	}

	/**
	 * 使用系统当前日期加以调整作为照片的名称
	 * 
	 * @return
	 */
	private String getPhotoFileName() {
		Date date = new Date(System.currentTimeMillis());
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault());
		return "IMG_" + dateFormat.format(date) + ".jpg";
	}

}
